package com.mydesign.demo.util;


import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;


public class ImageUtil implements Serializable {


	private static final long serialVersionUID = 1L;
//    private static String winImageUrl ="/fly/image/";
//
    private static String winVideoUrl ="/mydesign/video/";
//	private static String winDocumentUrl ="/fly/document/";
//	private static String winBookUrl ="/fly/book/";
//	private static String winEpubUrl ="/fly/epub/";
//	private static String winPdfUrl ="/fly/pdf/";
//	private static String wincacheUrl="/fly/cache/";

	private static String winImageUrl ="D:\\fly\\image\\";
	private static String winBaseImageUrl ="D:\\fly\\baseimage\\";
	//private static String winVideoUrl ="D:\\mydesign\\video\\";
	private static String winDocumentUrl ="D:\\fly\\document\\";
	private static String winBookUrl ="D:\\fly\\book\\";
	private static String winEpubUrl ="D:\\fly\\epub\\";
	private static String winPdfUrl ="D:\\fly\\pdf\\";
	private static String wincacheUrl="D:\\fly\\cache\\";
	String os = System.getProperty("os.name");




	/**
     * 判断是图片还是视频
     * @param fileName
     * @return
     */
    public static String fileType(String fileName) {
        String fileType = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()).toLowerCase();
        // 创建视频类型数组
        String video[] = {"mp4", "avi", "mov", "wmv", "asf", "navi", "3gp", "mkv", "f4v", "rmvb", "webm"};
        for (int i = 0; i < video.length; i++) {
            if (video[i].equals(fileType)) {
                return "video";
            }
        }
        // 创建图片类型数组
        String imgs[] = {"bmp", "jpg", "jpeg", "png"};
        for (int i = 0; i < imgs.length; i++) {
            if (imgs[i].equals(fileType)) {
                return "image";
            }
        }
        // 创建图片类型数组
        String img[] = {"bmp", "jpg", "jpeg", "png", "tiff", "gif", "pcx", "tga", "exif", "fpx", "svg", "psd", "cdr",
                "pcd", "dxf", "ufo", "eps", "ai", "raw", "wmf"};
        for (int i = 0; i < img.length; i++) {
            if (img[i].equals(fileType)) {
                return "img";
            }
        }
        return "error";
    }

    /**
     * 上传图片
     * @param file
     * @return
     */
    public static String uploadFileImage(MultipartFile file) {
        String fileName = file.getOriginalFilename();
        fileName = FileUtil.renameToUUID(fileName).replace("-", "");
        try {
            FileUtil.uploadFile(file.getBytes(),winVideoUrl , fileName);
		} catch (Exception e) {
            e.printStackTrace();
        }
        return "/img/" + fileName;
    }
	public static String uploadcache(MultipartFile file) {
		String fileName = file.getOriginalFilename();
		fileName = FileUtil.renameToUUID(fileName).replace("-", "");
		if(ImgzipUtils.isImage(cn.hutool.core.io.FileUtil.extName(fileName)))
		{
		try {
			FileUtil.uploadFile(file.getBytes(),winBaseImageUrl , fileName);
			ImgzipUtils.generateThumbnail2Directory(0.6d,wincacheUrl,winBaseImageUrl+fileName);
		} catch (Exception e) {
			e.printStackTrace();
		}
			return "/cache/" + fileName;
		}
		return null;

	}

	/**
	 * 上传视频
	 * @param file
	 * @return
	 */
	public static String uploadFileVideo(MultipartFile file) {
		String fileName = file.getOriginalFilename();
		fileName = FileUtil.renameToUUID(fileName).replace("-", "");
		try {
			FileUtil.uploadFile(file.getBytes(),winVideoUrl , fileName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/fly/" + fileName;
	}

	/**
	 * 上传文档
	 * @param file
	 * @return
	 */
	public static String uploadFileDocument(MultipartFile file) {
		String fileName = file.getOriginalFilename();
		fileName = FileUtil.renameToUUID(fileName).replace("-", "");
		try {
			FileUtil.uploadFile(file.getBytes(),winDocumentUrl , fileName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/fly/" + fileName;
	}

	/**
	 * 上传书籍
	 * @param file
	 * @return
	 */
	public static String uploadFileBook(MultipartFile file) {
		String fileName = file.getOriginalFilename();
		fileName = FileUtil.renameToUUID(fileName).replace("-", "");
		try {
			FileUtil.uploadFile(file.getBytes(),winBookUrl , fileName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/fly/" + fileName;
	}

	/**
	 * 上传epub文件
	 * @param file
	 * @return
	 */
	public static String uploadFileEpub(MultipartFile file) {
		String fileName = file.getOriginalFilename();
		fileName = FileUtil.renameToUUID(fileName).replace("-", "");
		try {
			FileUtil.uploadFile(file.getBytes(),winVideoUrl , fileName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return  "/fly/"+fileName;
	}
	/**
	 * 上传epub文件
	 * @param file
	 * @return
	 */
	public static String uploadFilePdf(MultipartFile file) {
		String fileName = file.getOriginalFilename();
		fileName = FileUtil.renameToUUID(fileName).replace("-", "");
		try {
			FileUtil.uploadFile(file.getBytes(),winPdfUrl , fileName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return  fileName;
	}

	/**
	 * 图片处理
	 * @param request
	 * @param fileName
	 * @return
	 */
	public static List<MultipartFile> getImgFile(HttpServletRequest request, String fileName) {
		List<MultipartFile> img = new LinkedList<MultipartFile>();
		//用于判断是普通表单，还是带文件上传的表单，起了辨别的作用。若返回值为true则是带文件上传的表单；返回值为false则是普通表单
		if (ServletFileUpload.isMultipartContent(request)) {
			MultipartHttpServletRequest multipartRequest = null;
			try {
				multipartRequest = (MultipartHttpServletRequest) request;
			} catch (Exception e) {

			}
			img = multipartRequest.getFiles(fileName);

		}
		return img;
	}
}
